package com.ifacebox.vcam4j;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author znn
 */
public class VCamRendererService implements Runnable {
    private static final Log log = LogFactory.getLog(VCamRendererService.class);
    private volatile boolean isRunning = false;
    private volatile boolean isOpening = false;
    private final VCamTakeService vCamTakeService;
    private VCamService vCamService;
    private Thread vCamThread;
    private String vCamIp;
    private int vCamFps;

    public VCamRendererService(String vCamIp, int vCamFps) {
        this.vCamIp = vCamIp;
        this.vCamFps = vCamFps;
        this.vCamTakeService = new VCamTakeService();
    }

    public void setVCamIp(String vCamIp) {
        this.vCamIp = vCamIp;
    }

    public void setVCamFps(int vCamFps) {
        this.vCamFps = vCamFps;
    }

    public boolean init() {
        try {
            this.vCamService = new VCamDriverService();
            if (log.isInfoEnabled()) {
                log.info(String.format("初始化虚拟摄像头：【%s】", this.vCamService.getName()));
            }
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error(String.format("初始化虚拟摄像头异常：【%s】", e.getMessage()), e);
            }
            this.vCamService = null;
        }
        if (this.vCamService == null) {
            try {
                this.vCamService = new VCamDShowService();
                if (log.isInfoEnabled()) {
                    log.info(String.format("初始化虚拟摄像头：【%s】", this.vCamService.getName()));
                }
            } catch (Exception e) {
                if (log.isErrorEnabled()) {
                    log.error(String.format("初始化虚拟摄像头异常：【%s】", e.getMessage()), e);
                }
                this.vCamService = null;
            }
        }
        return this.vCamService != null;
    }

    public void dispose() {
        if (this.vCamService != null) {
            this.vCamService.sendFrameEx(null);
            this.vCamService.dispose();
            this.vCamService = null;
        }
    }

    public void start() {
        if (this.vCamThread == null) {
            this.isRunning = true;
            this.vCamThread = new Thread(this, "VCam-Thread");
            this.vCamThread.setDaemon(true);
            this.vCamThread.start();
            if (log.isInfoEnabled()) {
                log.info(String.format("启动虚拟摄像头服务：【%s】", vCamService.getName()));
            }
        }
    }

    public void stop() {
        this.isRunning = false;
        if (this.vCamThread != null) {
            this.vCamThread.interrupt();
            this.vCamThread = null;
            if (log.isInfoEnabled()) {
                log.info(String.format("停止虚拟摄像头服务：【%s】", vCamService.getName()));
            }
        }
        this.dispose();
    }

    @Override
    public void run() {
        while (this.isRunning) {
            this.vCamService.sleepFps(this.vCamFps);
            if (this.vCamService.isOpened()) {
                if (!this.isOpening) {
                    if (log.isInfoEnabled()) {
                        log.info(String.format("打开虚拟摄像头：【%s】", vCamService.getName()));
                    }
                    this.isOpening = true;
                }
            } else {
                if (this.isOpening) {
                    if (log.isInfoEnabled()) {
                        log.info(String.format("关闭虚拟摄像头：【%s】", vCamService.getName()));
                    }
                    this.vCamService.sendFrameEx(null);
                    this.isOpening = false;
                }
                continue;
            }
            VCamTake vCamTake = this.vCamTakeService.take(this.vCamIp);
            this.vCamService.sendFrameEx(vCamTake == null ? null : vCamTake.getPhoto());
        }
    }

}
